package com.bridge.client.bridgedynamcclient.controller;

import com.alibaba.fastjson.JSON;
import com.bridge.client.bridgedynamcclient.bean.BridgeRecordItem;
import com.bridge.client.bridgedynamcclient.bean.BridgeRecordResultItem;
import com.bridge.client.bridgedynamcclient.bean.ChoiceItem;
import com.bridge.client.bridgedynamcclient.bean.vo.resp.BridgeRecordData;
import com.bridge.client.bridgedynamcclient.bean.vo.resp.ExtraImage;
import com.bridge.client.bridgedynamcclient.custom_compinents.TextImageView;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;

import java.net.URL;
import java.util.*;

public class BridgeAnalysisResultController extends BaseController implements Initializable {


    @FXML
    private TableColumn<?, ?> columnResultFirst;

    @FXML
    private TableColumn<?, ?> columnResultFour;

    @FXML
    private TableColumn<?, ?> columnResultSecond;

    @FXML
    private TableColumn<?, ?> columnResultThird;


    @FXML
    private TableView<BridgeRecordResultItem> tableResult;


    @FXML
    private AnchorPane lvResultImage;

    TextImageView textImageView = new TextImageView(633, 772);

    @FXML
    private Text txBridgeName;


    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        logger.info("initialize--");
        initListener();
        initTableView();
    }


    private void initTableView() {

        tableResult.setItems(FXCollections.observableArrayList());
        columnResultFirst.setCellValueFactory(new PropertyValueFactory<>("zxjs"));
        columnResultSecond.setCellValueFactory(new PropertyValueFactory<>("gypl"));
        columnResultThird.setCellValueFactory(new PropertyValueFactory<>("zxznb"));
        columnResultFour.setCellValueFactory(new PropertyValueFactory<>("mtzl"));


//        lvResultImage.setCellFactory(new Callback<ListView<ChoiceItem>, ListCell<ChoiceItem>>() {
//            @Override
//            public ListCell<ChoiceItem> call(ListView<ChoiceItem> choiceItemListView) {
//                return new ImageSampleListCell();
//            }
//        });
//        lvResultImage.setItems(FXCollections.observableArrayList());

        lvResultImage.getChildren().add(textImageView);
    }


    private static class ImageSampleListCell extends ListCell<ChoiceItem> {
        private Map<String, VBox> cacheItem = new HashMap<>();

        @Override
        protected void updateItem(ChoiceItem choiceItem, boolean empty) {
            super.updateItem(choiceItem, empty);
            System.out.println("updateItem = " + (choiceItem == null ? "空" : choiceItem.getName()));
            if (empty || choiceItem == null) {
                setText(null);
                setGraphic(null);
            } else {
                VBox vBox = cacheItem.get(choiceItem.getName());
                if (vBox == null) {
                    vBox = new VBox();
                    Text text = new Text(choiceItem.getName());
                    ImageView image = new ImageView();
                    //后台线程异步中获取
                    image.setImage(new Image(choiceItem.getValue(), true));
                    image.setFitHeight(400);
                    image.setFitWidth(400);
                    image.setPreserveRatio(true);
                    vBox.getChildren().addAll(image,text);
                    cacheItem.put(choiceItem.getName(), vBox);
                    System.out.println("updateItem 没有缓存。创建view= " + choiceItem.getName());
                } else {
                    System.out.println("updateItem 有缓存。无需创建view= " + choiceItem.getName());
                }
                setGraphic(vBox);
            }


        }
    }

    @Override
    public void setRoot(AnchorPane root) {
        super.setRoot(root);

    }

    private void initListener() {

    }

    public void setConfig(BridgeRecordItem item, BridgeRecordData.BridgeRecord bridgeRecord) {
        logger.info("setConfig");

        txBridgeName.setText("桥梁："+item.getBridgeName());
        //综合判断结果
        String resultJson = bridgeRecord.getResultJson();
        logger.info("setConfig,resultJson:{}", JSON.toJSONString(resultJson));
        List<BridgeRecordResultItem> resultItems = JSON.parseArray(resultJson, BridgeRecordResultItem.class);
        tableResult.getItems().addAll(resultItems);


        if (!bridgeRecord.getImages().isEmpty()) {

            List<TextImageView.ImageData> imageDataList = new ArrayList<>();
            for (ExtraImage image : bridgeRecord.getImages()) {
                imageDataList.add(new TextImageView.ImageData(image.getName(), image.getUrl(), 2));
            }
            textImageView.setImages(imageDataList, 500, 500);
        }


    }
}
